package cn.kgc.crowdfundingadmin.service;

import cn.kgc.crowdfundingadmin.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin.pojo.Admin;
import cn.kgc.crowdfundingadmin.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;


    @Override
    public Admin login(String loginAcct, String userPswd) {
        //根据loginacct查询admin
        //判断是否为空
        //如果不为空给密码加密
        //和数据库的密码比较
        //如果一样,登录成功
        Admin admin=null;
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);

        List<Admin> list=adminMapper.selectByExample(example);
        if (CrowdFundingUtils.collectionEffective(list)){
            admin=list.get(0);
            String md5 = CrowdFundingUtils.md5(userPswd);
            if (md5.equals(admin.getUserpswd())){
                //成功
                return  admin;
            }else {
                return null;
            }
        }
        return admin;
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        //设置当前页
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        //keyword没有值查询全部,有值根据账号或者名称或者邮箱地址牧户查询
        if(CrowdFundingUtils.stringEffective(keyword)){
            AdminExample example=new AdminExample();
            AdminExample.Criteria criteria1=example.createCriteria();
            criteria1.andLoginacctLike("%"+keyword+"%");
            AdminExample.Criteria criteria2=example.createCriteria();
            criteria2.andUsernameLike("%"+keyword+"%");
            AdminExample.Criteria criteria3=example.createCriteria();
            criteria3.andEmailLike("%"+keyword+"%");

            example.or(criteria2);
            example.or(criteria3);

            List<Admin> list=adminMapper.selectByExample(example);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }else{
            //查询全部
            List<Admin> list=adminMapper.selectByExample(null);
            PageInfo<Admin> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }
    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);
        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        //密码
        String userPswd = admin.getUserpswd();
        //加密密码
        /*userPswd = CrowdFundingUtils.md5(userPswd);*/
        userPswd=bCryptPasswordEncoder.encode(userPswd);
        ///获取密码
        admin.setUserpswd(userPswd);

        // 执行保存
        adminMapper.insert(admin);
    }

    //回显,根据id查询
    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        if(CrowdFundingUtils.stringEffective(admin.getUserpswd())){
                String userPswd=admin.getUserpswd();
          /*  userPswd = CrowdFundingUtils.md5(userPswd);*/
          userPswd=bCryptPasswordEncoder.encode(userPswd);
            admin.setUserpswd(userPswd);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }
}

